<?php
/**
 * Created by PhpStorm.
 * User: hanbin
 * Date: 17/5/31
 * Time: 上午10:22
 */

namespace App\Http\Controllers\Zhihu;


use App\Logic\zhihu\OrderLogic;
use Validator;

/**
 * 订单
 * Class OrderController
 * @package App\Http\Controllers\Zhihu
 */
class OrderController extends BaseController
{
    public function __construct()
    {
        $this->setToken(true);
        parent::__construct();
    }


    /**
     * 订单列表
     *
     * @return array
     */
    public function getList()
    {
        $param = file_get_contents("php://input");
        $params = json_decode($param, true);
        $rule = [
            'pagesize' => 'required|int',
            'last_id' => 'required|int',
        ];
        $message = [
            'pagesize.required' => '每页个数不能为空',
            'last_id.required' => '页码不能为空',
        ];
        $validator = Validator::make($params, $rule, $message);
        $errors = $validator->errors()->all();
        if ($errors && !$validator->passes()) {
            return $this->error($errors[0], 206);
        }
        $params['member_id'] = $this->userId;
        return OrderLogic::getList($params);
    }

    /**
     * 下单
     * @return array
     */
    public function place(){
        $param = file_get_contents("php://input");
        $params = json_decode($param, true);
        $rule = [
            'service_id' => 'required|int',
            'orderprice' => 'required|numeric',
            'ordernumber' => 'required|int',
        ];
        $message = [
            'service_id.required' => '服务不能为空',
            'orderprice.required' => '单价不能为空',
            'ordernumber.required' => '购买数量不能为空',
        ];
        $validator = Validator::make($params, $rule, $message);
        $errors = $validator->errors()->all();
        if ($errors && !$validator->passes()) {
            return $this->error($errors[0], 206);
        }
        $params['member_id'] = $this->userId;
        return OrderLogic::place($params);
    }


}